<?php
class BaseAction
{
	public static $plug = array ();
	
	function __construct()
	{
		require_once BASE_PATH . 'library/BaseFormCheck.class.php';
	}
	
	/**
	 * 载入视图
	 * @param string $tpl
	 * @param array $array
	 * @param boolean $return
	 */
	public static final function loadView($tpl, $array = array(), $return = false)
	{
		$data = array ();
		$laodTemp = BaseTemplate::loadTemplate ( $tpl, $array, $data );
	}
	
	/**
	 * 载入成功模版
	 * @param string $msg
	 * @param string $url
	 */
	public static final function success($message, $buttonUrl = FALSE, $buttonMessage = "点击这里返回")
	{
		ViewData::$body ['buttonMessage'] = $buttonMessage;
		ViewData::$body ['buttonUrl'] = $buttonUrl === FALSE ? $_SERVER ['HTTP_REFERER'] : $buttonUrl;
		ViewData::$body ['message'] = $message;
		self::loadView ( "successMessage" );
	}
	
	/**
	 * 载入错误处理模版
	 * @param string $msg
	 */
	public static final function error($msg = '系统发生错误，操作失败!请稍后重试！',$buttonUrl = FALSE, $buttonMessage = "点击这里返回")
	{
		ViewData::$body ['message'] = $msg;
		
		if($buttonUrl===FALSE){
			if(isset($_SERVER ['HTTP_REFERER'])){
				$buttonUrl=$_SERVER ['HTTP_REFERER'];
			}else{
				$buttonUrl=siteUrl();
			}
		}
		ViewData::$body ['buttonUrl'] = $buttonUrl ;
		ViewData::$body ['buttonMessage'] = $buttonMessage;
		self::loadView ( "errorMessage" );
	}
	
/**
	 * 载入后台成功模版
	 * @param string $msg
	 * @param string $url
	 */
	public static final function adminSuccess($message, $buttonUrl = FALSE, $buttonMessage = "点击这里返回")
	{
		ViewData::$body ['buttonMessage'] = $buttonMessage;
		ViewData::$body ['buttonUrl'] = $buttonUrl === FALSE ? $_SERVER ['HTTP_REFERER'] : $buttonUrl;
		ViewData::$body ['message'] = $message;
		self::loadView ( "admin/successMessage" );
	}
	
	/**
	 * 载入后台错误处理模版
	 * @param string $msg
	 */
	
	/**
	 * 载入错误处理模版
	 * @param string $msg
	 */
	public static final function adminError($msg = '系统发生错误，操作失败!请稍后重试！',$buttonUrl = FALSE, $buttonMessage = "点击这里返回")
	{
		ViewData::$body ['message'] = $msg;
		if($buttonUrl===FALSE){
			if(isset($_SERVER ['HTTP_REFERER'])){
				$buttonUrl=$_SERVER ['HTTP_REFERER'];
			}else{
				$buttonUrl=siteUrl();
			}
		}
		ViewData::$body ['buttonUrl'] = $buttonUrl ;
		ViewData::$body ['buttonMessage'] = $buttonMessage;
		self::loadView ( "admin/errorMessage" );
	}
	
	
	public static final function redirect($url = '')
	{
		header ( "Location:" . siteUrl ( $url ) );
	}
	
	/**
	 * 检测表单字段
	 * @param sting $name post或则GET的字段
	 * @param string $post 获取的方式
	 * @param string $msg 如果为空的时候的提示文字
	 * @param array $checkLen 数组中第一个表示类型，第二个表示最小值，第三个表示最大值，第四个为出错的时候的提示语言
	 * @param array $oterhFun 第一个为校验的函数，第二个为出错的时候的提示语言
	 * @param boolen $ajax 为false 时直接弹出报错，true直接返回，由form层控制报错方式
	 * @throws Exception
	 */
	public static final function checkForm($name, $post, $msg, $checkLen, $oterhFun = false, $ajax = false)
	{
		try
		{
			$message = BaseFormCheck::formField ( $name, $post, $msg, $checkLen, $oterhFun);
			if($message){
				if ($ajax)
				{
					return $message;
				}else{
					throw new Exception($message);
				}
			}
		} catch ( Exception $e )
		{
			throw new Exception($e->getMessage());
		}
	}
	
	public static final function parseForm($method, $object)
	{
		switch ($method)
		{
			case "POST" :
				foreach ( $_POST as $key => $value )
				{
					$object->$key = $value;
				}
				return $object;
				break;
			case "GET" :
				foreach ( $_GET as $key => $value )
				{
					$object->$key = $value;
				}
				return $object;
				break;
			default :
				break;
		}
		return FALSE;
	}
	public static final function isObjectEmpty($object)
	{
		if (! is_object ( $object ))
		{
			return FALSE;
		}
		foreach ( $object as $value )
		{
			if ($value)
			{
				return FALSE;
			}
		}
		return TRUE;
	}
}

class ViewData
{
	public static $title="我需要标题";
	public static $keywords="我需要关键字";
	public static $description="我需要描述";
	public static $page = '';
	public static $body = '';
	public static $form_data = '';
	public static $form_hint = '';
}